﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using System;
using ASPNETMVCApplication.Code.Filters;
using ASPNETMVCApplication.Repositories;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Code.HtmlHelpers;
using ASPNETMVCApplication.Controllers;
using ASPNETWebApplication;
using ASPNETMVCApplication.Areas.Supp.Models;

namespace ASPNETMVCApplication.Areas.Supp.Controllers
{
    /// <summary>
    /// Controller class for the Shopping area.
    /// </summary>
    public class OrderController : BaseController
    {
        private IOrderRepository _orderRepository;
        private IProductRepository _productRepository;

        public OrderController()
            : this(new OrderRepository(), new ProductRepository())
        {
        }

        /// <summary>
        /// Overloaded 'injectable' Constructor for ShopController.
        ///
        /// Pattern: Constructor Dependency Injection (DI).
        /// </summary>
        /// <param name="categoryRepository"></param>
        /// <param name="productRepository"></param>
        /// <param name="cartRepository"></param>
        public OrderController(IOrderRepository orderRepository, IProductRepository productRepository)
        {
            _orderRepository = orderRepository;
            _productRepository = productRepository;
        }

        [Menu(MenuItem.Orders)]
        public ActionResult CustIndex()
        {
            var CustId = (int)Session["custId"];
            var listOrder = _orderRepository.GetOrderList(CustId,0,DateTime.Now,DateTime.Now,0);
            return View(listOrder);
        }

        [Menu(MenuItem.Orders)]
        public ActionResult SuppIndex()
        {
            var SuppId = (int)Session["custId"];
            var listOrder = _orderRepository.GetOrderList(0, SuppId, DateTime.Now, DateTime.Now, 0);
            return View(listOrder);
        }

        public ActionResult ViewOrderDetail(int OrderId)
        {
            var Order = _orderRepository.Get(OrderId);
            return View(Order);
        }

        public string OrderApprove(Order order)
        {
            try
            {
                OrderDetail[] arrayDetail = order.OrderDetails;
                //cập nhật trạng thái vé
                if (arrayDetail.Count() > 0)
                {
                    var detail = new OrderDetail();

                    for (int i = 0; i < arrayDetail.Count(); i++)
                    {
                        Product product = new Product();
                        product.CategoryId = detail.CategoryId;
                        product.ProductId = 0;
                        product.Status = detail.Status;
                        _productRepository.Update(product);
                    }
                }

                Order orderHeader = new Order();
                orderHeader.CustomerId = order.CustomerId;
                orderHeader.OrderDate = order.OrderDate;
                orderHeader.RequiredDate = DateTime.Now;
                orderHeader.Freight = 0;
                orderHeader.Status = 1;
                orderHeader.OrderDetails = arrayDetail;

                _orderRepository.Update(orderHeader);
                return "Đã bán vé thành công";
            }
            catch (Exception ex)
            {
                return "Bán vé không thành công";
            }

        }

    }
}


